const mongoose = require('mongoose')

const DB_URL = 'mongodb://localhost:27017/imooc'
//{ useFindAndModify: false }
mongoose.connect(DB_URL, { useNewUrlParser: true, useFindAndModify: false })
mongoose.connection.on('connected', () => {
    console.log('mongo connect success')
})

const models = {
    user: {
        username: { type: String, require: true },
        password: { type: String, require: true },
        type: { type: String, require: true },
        avatar: { type: String },
        desc: { type: String },
        title: { type: String },
        company: { type: String },
        money: { type: String },
    },
    chat: {
        group: { type: String, require: true },
        from: { type: String, require: true },
        to: { type: String, require: true },
        read: { type: Boolean, default: false },
        content: { type: String, require: true },
        time: { type: Number, require: true }
    }
}

for(let key in models){
    mongoose.model(key, new mongoose.Schema(models[key]))
}

module.exports = {
    getModel: function(key){
        return mongoose.model(key)
    }
}